#version 300 es
uniform mat4 uMVPMatrix ;
uniform mat4 uMMatrix;

uniform vec3 uLightLocation;
uniform vec3 uCamera;

in vec3 aPosition;
in vec3 aNormal;
in vec2 aTexCoors ;

out vec2 vTexCoors ;
out vec4 vAmbient;   //环境光
out vec4 vDiffuse;   //散射光
out vec4 vSpecular;  //镜面光
out vec4 vPosition;

void point(){ //环境光设置
    vAmbient = vec4(0.15,0.15,0.15,1.0);
}

void point1(
in vec3 normal,
inout vec4 diffuse,
in vec3 lightLocation,
in vec4 lightDiffuse
){ //散射光设置
vec3 normalTarget=aPosition+normal;
vec3 newNormal=(uMMatrix*vec4(normalTarget,1)).xyz-(uMMatrix*vec4 (aPosition,1)).xyz;
newNormal=normalize(newNormal);
vec3 vp= normalize(lightLocation-(uMMatrix*vec4(aPosition,1)).xyz);
vp=normalize(vp);
float nDotViewPosition=max(0.0,dot(newNormal,vp));
diffuse=lightDiffuse*nDotViewPosition;
}

void point2(
in vec3 normal,
inout vec4 specular,
in vec3 lightLocation,
in vec4 lightSpecular
){ // 镜面光设置
vec3 normalTarget=aPosition+normal;  //
vec3 newNormal=(uMMatrix*vec4(normalTarget,1)).xyz-(uMMatrix*vec4(aPosition, 1)).xyz;
newNormal=normalize(newNormal);    //
vec3 eye= normalize(uCamera-(uMMatrix*vec4(aPosition,1)).xyz);//
vec3 vp= normalize(lightLocation-(uMMatrix*vec4(aPosition,1)).xyz);//
vp=normalize(vp);//
vec3 halfVector=normalize(vp+eye);//
float shininess=50.0;//
float nDotViewHalfVector=dot(newNormal,halfVector);//
float powerFactor=max(0.0,pow(nDotViewHalfVector,shininess));//
specular=lightSpecular*powerFactor;//
}

void test(){
    point() ;
}

void test1(){

    vec4 diffuseTemp=vec4(0.0,0.0,0.0,0.0);
    point1(normalize(aNormal), diffuseTemp, uLightLocation, vec4(0.8,0.8,0.8,1.0));
    vDiffuse=diffuseTemp;

}
void test2(){
    vec4 specularTemp=vec4(0.0,0.0,0.0,0.0);
    point2(normalize(aNormal), specularTemp, uLightLocation, vec4(0.7,0.7,0.7,1.0));
    vSpecular=specularTemp;
}

void main(){
    gl_Position = uMVPMatrix * vec4(aPosition , 1) ;
            test();
            test1();
            test2();
            vTexCoors  = aTexCoors ;
vPosition=uMMatrix*vec4(aPosition,1);
}